<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\AdminModel;
use App\Model\SysModel;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class CommonDao extends BaseDao
{
    public static function goods(): array
    {
        $datas = [
            ['title'=>'Clover 3672 Amour Crochet Hook Set','u_price'=>49.8,'days'=>10,'shouyi'=>52,'danshu'=>1,'image'=>'https://kehus.oss-cn-hangzhou.aliyuncs.com/20240909092033-1725844833.jpg'],
            ['title'=>'Fitness equipment, brand selection','u_price'=>98.3,'days'=>10,'shouyi'=>104,'danshu'=>2,'image'=>'https://kehus.oss-cn-hangzhou.aliyuncs.com/20240909092127-1725844887.jpg'],
            ['title'=>'Logitech Harmony One Universal','u_price'=>196.5,'days'=>10,'shouyi'=>208,'danshu'=>4,'image'=>'https://kehus.oss-cn-hangzhou.aliyuncs.com/20240908223534-1725806134.jpg'],
            ['title'=>'Citizen Mens Eco-Drive','u_price'=>485.6,'days'=>10,'shouyi'=>518,'danshu'=>10,'image'=>'https://kehus.oss-cn-hangzhou.aliyuncs.com/20240909092240-1725844960.jpg'],
            ['title'=>'MSI Raider GE76 Gaming Laptop','u_price'=>985.2,'days'=>10,'shouyi'=>1036,'danshu'=>20,'image'=>'https://kehus.oss-cn-hangzhou.aliyuncs.com/20240909092333-1725845013.jpg'],
        ];
        foreach ($datas as &$value){
            $value['shouyi'] = bcmul($value['u_price'],0.04,2);
        }
        return  $datas;
    }

    public static function datas()
    {
        return [
            'all_orders'=>OrdersDao::builder_query()->count(),
            'order_re'=>OrdersDao::builder_query()->where('u_status','>',0)->count(),
        ];
    }

    //自动托管
    public static function auto_tuo(array $params)
    {
        checkForData(['auto_money'],['请输入托管金额'],$params);
        $userInfo = UsersDao::builder_query([
            'user_id'=>$params['user_id']
        ])->first()->toArray();
        if($userInfo['u_price'] < $params['auto_money']){
            self::error('金额不足,无法托管');
        }
        if($userInfo['auto_u_price'] > 0){
            self::error('已经有金额正在托管');
        }
        //自动托管后进行保存
        //扣款并且转到托管
        DB::beginTransaction();
        try {
            BillsDao::moneyUpdate(-($params['auto_money']),$params['user_id'],'自动托管扣费');
            UsersDao::builder_query(['user_id'=>$params['user_id']])->increment('auto_u_price',$params['auto_money']);
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error($exception->getMessage());
        }

    }

    public static function settingSet(array $params)
    {
        DB::beginTransaction();
        try {
            SysModel::query()->where('key_name','recharge_url1')->update([
                'key_text'=>$params['recharge_url1']
            ]);
            SysModel::query()->where('key_name','recharge_url2')->update([
                'key_text'=>$params['recharge_url2']
            ]);
            if(checkIsHasField($params,'password')){
                AdminModel::query()->where('username','admin')->update([
                    'password'=>password_hash($params['password'],PASSWORD_DEFAULT)
                ]);
                Cookie::queue('dailian_u_id','');//设置缓存
            }
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error($exception->getMessage());
        }
    }
}
